import { useEffect, useRef } from "react"

/**
 * 防抖
 *
 * @param {*} func
 * @param {*} wait
 * @param {*} immediate
 * @returns
 */
export const useDebounce = <T extends (...args: any) => any>(
  fn: T,
  time?: number,
): ((...args: any) => any) => {
  const timeoutIdRef = useRef<NodeJS.Timeout>()
  let delay = time || 300
  return (...args: any) => {
    if (timeoutIdRef.current) {
      clearTimeout(timeoutIdRef.current)
    }
    timeoutIdRef.current = setTimeout(() => {
      fn(...args)
    }, delay)
  }
}
